package hlhj.fhp.burst.adapter

import android.content.Context
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import hlhj.fhp.burst.EmptyUtil
import hlhj.fhp.burst.R
import hlhj.fhp.burst.bean.BurstListBean
import java.util.ArrayList

/**
 * Created by Administrator on 2018\5\16 0016.
 */
class CommentAdp(private val context: Context,private val datas:ArrayList<BurstListBean.DataBean.ListBean.CommentBean>) :BaseAdapter() {
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var convertView= convertView
        if (convertView==null){
            convertView=LayoutInflater.from(context).inflate(R.layout.comment_item, null)
        }
        val talker = convertView?.findViewOften<TextView>(R.id.talker)
        val tv = convertView?.findViewOften<TextView>(R.id.tv)
        val replayer = convertView?.findViewOften<TextView>(R.id.replayer)
        val tvContent = convertView?.findViewOften<TextView>(R.id.itemContent)


        val bean = datas[position]
        if (bean?.member_name==null||bean?.member_name.isEmpty()){
            talker?.visibility=View.GONE
            tv?.visibility=View.GONE
            tvContent?.text=bean?.content
            if (null==bean?.one_member_nickname||bean?.one_member_nickname.isEmpty()){
                replayer?.text=bean?.one_member_name+":"
            }else{
                replayer?.text=bean?.one_member_nickname+":"
            }
        }else{
            talker?.visibility=View.VISIBLE
            tv?.visibility=View.VISIBLE
            if (EmptyUtil.isStringNull(bean?.one_member_nickname)){
                replayer?.text=bean?.one_member_name
            }else{
                replayer?.text=bean?.one_member_nickname
            }
            if (EmptyUtil.isStringNull(bean?.member_nickname)){
                talker?.text=bean?.member_name
            }else{
                talker?.text=bean?.member_nickname
            }
            tvContent?.text=bean?.content
        }

        return convertView!!
    }

    override fun getItem(position: Int): Any {
        return datas[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getCount(): Int {
        return  datas.size
    }
    fun <T : View> View.findViewOften(viewId: Int): T {
        var viewHolder: SparseArray<View> = tag as? SparseArray<View> ?: SparseArray()
        tag = viewHolder
        var childView: View? = viewHolder.get(viewId)
        if (null == childView) {
            childView = findViewById(viewId)
            viewHolder.put(viewId, childView)
        }
        return childView as T
    }
}